﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legazpi.Payroll.Services.Interfaces;
using System.Data.Entity;
using Emz.Core.Repositories;
using Emz.Core.Helpers;
using Emz.Core.Models;
using Emz.Core.Base;

namespace Legazpi.Payroll.Services.Services
{
    public class SSSTableService : ISSSTableService
    {

        #region Fields

        private DbContext _dbContext;
        private ISSSTableRepository _sssRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public SSSTableService()
            : this(new DatabaseFactory())
        {

        }

        public SSSTableService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _sssRepository = new SSSTableRepository(_dbContext);
            }
        }

        #endregion

        public SSSTable GetById(int id)
        {
            return _sssRepository.GetById(id);
        }

        public IEnumerable<SSSTable> GetAll()
        {
            return _sssRepository.GetAll();
        }

        public IEnumerable<SSSTable> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(SSSTable sssTableItem)
        {
            _sssRepository.Add(sssTableItem);
        }

        public void Update(SSSTable sssTableItem)
        {
            _sssRepository.Update(sssTableItem);
        }

        public void Delete(int id)
        {
            var sssTableItem = GetById(id);
            if (sssTableItem != null)
                _sssRepository.Delete(sssTableItem);
        }

        public bool Save()
        {
            string message;
            _sssRepository.Save(out message);
            return message == "Saved";
        }
    }
}
